from selenium import webdriver


import yaml
import os

class YamlReader:
    def __init__(self, filepath):
        self.filepath = filepath
        self.data = None
        self.read_yaml()

    def read_yaml(self):
        with open(self.filepath, 'r') as file:
            self.data = yaml.safe_load(file)

    def get_data(self):
        return self.data


class BasePage:
    __DRIVER = None
    def __init__(self):

        if BasePage.__DRIVER == None:

            filepath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'config/config.yaml')
            reader = YamlReader(filepath)
            data = reader.get_data()
            hub_url = data.get("selenium_server")

            # 定义要使用的浏览器 - 在这种情况下是 Chrome
            options = webdriver.ChromeOptions()

            options.set_capability("browserName", data.get("browser"))
            options.set_capability("platformName",data.get("platform"))

            # 创建一个 Remote WebDriver 实例
            driver = webdriver.Remote(command_executor=hub_url, options=options)
            driver.implicitly_wait(5)
            driver.maximize_window()
            BasePage.__DRIVER = driver

        self.driver = BasePage.__DRIVER


